home *** CD-ROM | disk | FTP | other *** search
/ Video Toaster 4.2 / Video Toaster v4.2.iso / arexx / modeler / coordsys.lwm < prev    next >
Text File  |  1993-12-13  |  6KB  |  175 lines

  1. /* CMD: Coordinate Systems
  2.  * Polar Coordinate systems conversion utility  */
  3. /* By Arnie Cachelin © 1992, 1993 NewTek Inc. */
  4.  
  5.  
  6. Pi=3.14159265358
  7. DegreesPerRadian= 180/pi
  8. Continue=1
  9. libadd = addlib("LWModelerARexx.port",0)
  10. signal on error
  11. signal on syntax
  12.  
  13. RexxMathLib = "rexxmathlib.library"
  14. IF POS(RexxMathLib , SHOW('Libraries')) = 0 THEN
  15.   IF ~ADDLIB(RexxMathLib , 0 , -30 , 0) THEN DO
  16.     call notify(1,"!Can't find rexxmathlib.library")
  17.     exit
  18.     end
  19. call req_begin "Coordinate System Calculator"
  20.  
  21. TxId = req_addcontrol("Enter coordinate",'T','values into appropriate "From" row')
  22. TxId = req_addcontrol("then click OK to",'T','fill in values for other systems. ')
  23. FromId = req_addcontrol("Convert From",'CH','Cartesian Spherical Cylindrical')
  24. CartId = req_addcontrol("Cartesian (X, Y, Z)",'V',0)
  25. SphereId = req_addcontrol("Spherical (R, Theta°, Phi°)",'V',0)
  26. CylId = req_addcontrol("Cylindrical (R, Theta°, Z)",'V',0)
  27.  
  28. call req_setval FromId,2,2
  29. call req_setval CartId, '0 0 0',0
  30. call req_setval CylId, '0 0 0',0
  31. call req_setval SphereId, '0 0 0',0
  32.  
  33. do while Continue  /* Keep Calculating until 'CANCEL' */
  34.     x = req_post()
  35.     if (x) then do
  36.         FromType=req_getval(FromId)
  37.         select
  38.           when FromType=1 then do
  39.             Vec=req_getval(CartId)
  40.             Vec=translate(Vec,',',' ')
  41.             interpret 'v1=Sphere_R('Vec')'
  42.             interpret 'v2=Sphere_Theta('Vec')'
  43.             interpret 'v3=Sphere_Phi('Vec')'
  44.             call req_setval SphereId, v1 v2 v3
  45.             interpret 'v1=Cyl_R('Vec')'
  46.             interpret 'v2=Cyl_Theta('Vec')'
  47.             interpret 'v3=Cyl_Z('Vec')'
  48.             call req_setval CylId, v1 v2 v3
  49.             end
  50.           when FromType=2 then do
  51.             Vec=req_getval(SphereId)
  52.             Vec=translate(Vec,',',' ')
  53.             interpret 'v1=Sphere_X('Vec')'
  54.             interpret 'v2=Sphere_Y('Vec')'
  55.             interpret 'v3=Sphere_Z('Vec')'
  56.             call req_setval CartId, v1 v2 v3
  57.             interpret 'v1=Sphere_X('Vec')'
  58.             interpret 'v2=Sphere_Y('Vec')'
  59.             interpret 'v3=Sphere_Z('Vec')'
  60.             Vec=v1','v2','v3
  61.             interpret 'v1=Cyl_R('Vec')'
  62.             interpret 'v2=Cyl_Theta('Vec')'
  63.             interpret 'v3=Cyl_Z('Vec')'
  64.             call req_setval CylId, v1 v2 v3
  65.             end
  66.           when FromType=3 then do
  67.             Vec=req_getval(CylId)
  68.             Vec=translate(Vec,',',' ')
  69.             interpret 'v1=Cyl_X('Vec')'
  70.             interpret 'v2=Cyl_Y('Vec')'
  71.             interpret 'v3=Cyl_Z('Vec')'
  72.             call req_setval CartId, v1 v2 v3
  73.             interpret 'v1=Cyl_X('Vec')'
  74.             interpret 'v2=Cyl_Y('Vec')'
  75.             interpret 'v3=Cyl_Z('Vec')'
  76.             Vec=v1','v2','v3
  77.             interpret 'v1=Sphere_R('Vec')'
  78.             interpret 'v2=Sphere_Theta('Vec')'
  79.             interpret 'v3=Sphere_Phi('Vec')'
  80.             call req_setval SphereId, v1 v2 v3
  81.             end
  82.           otherwise do
  83.             call req_end()
  84.             exit
  85.             end
  86.           end
  87.         end
  88.     else do
  89.        call req_end()
  90.        exit
  91.        end
  92. end
  93. call req_end()
  94. exit
  95.  
  96. syntax:
  97. error:
  98.     t=Notify(1,'!Rexx Script Error','@'ErrorText(rc),'Line 'SIGL)
  99.   call end_all
  100.   if (libadd) then call remlib("LWModelerARexx.port")
  101.     exit
  102.  
  103. /*
  104.    The following functions convert between 3D cartesian coordinates (X,Y,Z)
  105.    and either cylindrical coordinates (R,Theta,Z) or Spherical
  106.    coordinates (R,Theta,Phi).  The Cylindrical coordinate conversions don't
  107.    take Z inputs, since Z is the same in both cylindical and cartesian systems.
  108.    To go between spherical and cylindrical, use cartesian... or write your own.
  109.     p.s. I lied... see Cyl_Z(), z args are ok now too.
  110.  
  111. */
  112.  
  113.  
  114. /* Return R in Cylindrical coordinate system */
  115. Cyl_R: PROCEDURE
  116.   arg xf, yf, zf
  117.   return sqrt(xf*xf+yf*yf)
  118.  
  119. Cyl_Z: PROCEDURE
  120.   arg xf, yf, zf
  121.   return zf
  122.  
  123. /* Return Theta in Cylindrical coordinate system */
  124. Cyl_Theta: PROCEDURE EXPOSE DegreesPerRadian Pi
  125.   arg x, y, z
  126.     if x=0 then t=sign(y)*90
  127.   else t=DegreesPerRadian*atan(y/x)
  128.     if x<0 then t=t + 180 /* atan() doesn't know which quadrant you're in */
  129.   if t<0 then t=t + 360 /* atan() returns -180 to 180, I like 0 to 360 */
  130.   return t
  131.  
  132. /* Return Cartesian X from Cylindrical coordinate system */
  133. Cyl_X: PROCEDURE EXPOSE DegreesPerRadian
  134.   arg R, Theta, Z
  135.   return R*cos(Theta/DegreesPerRadian)
  136.  
  137. /* Return Cartesian Y from Cylindrical coordinate system */
  138. Cyl_Y: PROCEDURE EXPOSE DegreesPerRadian
  139.   arg R, Theta, Z
  140.   return R*sin(Theta/DegreesPerRadian)
  141.  
  142. /* Return R in Spherical coordinate system */
  143. Sphere_R: PROCEDURE
  144.     arg x, y, z
  145.   return sqrt(x*x+y*y+z*z)
  146.  
  147. /* Return Theta in Spherical coordinate system */
  148. Sphere_Theta: PROCEDURE EXPOSE DegreesPerRadian Pi
  149.   arg x, y, z
  150.     if x=0 then t=sign(y)*90
  151.   else t=DegreesPerRadian*atan(y/x)
  152.     if x<0 then t=t + 180 /* atan() doesn't know which quadrant you're in */
  153.   if t<0 then t=t + 360 /* atan() returns -180 to 180, I like 0 to 360 */
  154.   return t
  155.  
  156. /* Return Phi in Spherical coordinate system */
  157. Sphere_Phi: PROCEDURE EXPOSE DegreesPerRadian Pi
  158.   arg x, y, z
  159.     if z=0 then return 90
  160.     else return DegreesPerRadian*atan(sqrt(x*x+y*y)/z)
  161.  
  162. /* Return Cartesian X from Spherical coordinate system */
  163. Sphere_X: PROCEDURE EXPOSE DegreesPerRadian
  164.   arg R, Theta, Phi
  165.   return R*sin(abs(Phi)/DegreesPerRadian)*cos(Theta/DegreesPerRadian)
  166.  
  167. /* Return Cartesian Y from Spherical coordinate system */
  168. Sphere_Y: PROCEDURE EXPOSE DegreesPerRadian
  169.   arg R, Theta, Phi
  170.   return R*sin(abs(Phi)/DegreesPerRadian)*sin(Theta/DegreesPerRadian)
  171.  
  172. /* Return Cartesian Z from Spherical coordinate system */
  173. Sphere_Z: PROCEDURE EXPOSE DegreesPerRadian
  174.   arg R, Theta, Phi
  175.   return sign(Phi)*R*cos(Phi/DegreesPerRadian)